// 时间轴动画效果
function initTimelineAnimation() {
    // 检查是否存在 timeline-section 元素
    const timelineSection = document.querySelector('.timeline-section');
    if (!timelineSection) {
        console.log('Timeline section not found, skipping animation initialization');
        return;
    }
    console.log('Timeline animation initialized');

    const timelineItems = document.querySelectorAll('.timeline-item');
    // 初始化动画状态
    function initAnimation() {
        timelineItems.forEach(item => {
            item.style.opacity = '0';
            item.style.transform = 'translateY(20px)';
        });
    }
    // 执行动画
    function playAnimation() {
        timelineItems.forEach((item, index) => {
            setTimeout(() => {
                item.style.transition = 'all 0.8s ease-out';
                item.style.opacity = '1';
                item.style.transform = 'translateY(0)';
            }, 300 * (index + 1)); // 每个项目延迟300ms
        });
    }
    // 检查元素是否在视口中
    function isInViewport(element) {
        const rect = element.getBoundingClientRect();
        const windowHeight = window.innerHeight || document.documentElement.clientHeight;
        const windowWidth = window.innerWidth || document.documentElement.clientWidth;
        const threshold = windowHeight * 0.2;
        return (
            rect.top <= windowHeight - threshold &&
            rect.bottom >= threshold &&
            rect.left <= windowWidth &&
            rect.right >= 0
        );
    }
    // 监听滚动事件
    function handleScroll() {
        if (timelineSection && isInViewport(timelineSection)) {
            playAnimation();
            window.removeEventListener('scroll', handleScroll);
        }
    }
    // 处理窗口大小变化
    function handleResize() {
        initAnimation();
        handleScroll();
    }
    // 初始化
    initAnimation();
    window.addEventListener('scroll', handleScroll);
    window.addEventListener('resize', handleResize);
    handleScroll();
}

if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initTimelineAnimation);
} else {
    initTimelineAnimation();
}
